package com.itheima.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
    @WebFilter( "/*") //所有网址映射都生效的过滤器
    public class CharacterEncodingFilter implements Filter {
        private String encoding="UTF-8";
        @Override
        public void init(FilterConfig filterConfig) {
            //System.out.println("过滤器初始化");
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            request.setCharacterEncoding(encoding);// 设置请求类型为UTF-8
            response.setContentType("text/html; charset=" + encoding);// 设置请求类型为UTF-8
            //System.out.println("过滤器开始");
            chain.doFilter(request, response);
            //System.out.println("过滤器结束");
        }
        @Override
        public void destroy() {
            //System.out.println("过滤器销毁");
        }
    }

